const express = require("express")
const cors = require('cors');
const app = express();
app.use(cors());
const middleware = require("./middleware/middleware")//中间件

app.use(express.json()); //解析接受的json数据

app.use((req, res, next) => {
    res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, private');
    next();
});

// app.use(express.urlencoded({extended:false}))//解析接收的formdata数据

const response = require("./utils/response") // 自定义响应方法

app.use((req, res, next) => { // 全局中间件
    // console.log("全局中间件")


    for (const key in response) {
        res[key] = response[key]
    }



    next()
})


const customMiddleware = (req, res, next) => {//局部中间件
    // console.log("局部中间件")
    next()
}


// const mysql = require("./initDB")
// mysql.InitDB()

const user = require("./router/user")
const ranking = require("./router/ranking")
app.use("/api/v1/uploadScore", middleware.jwt);
app.use("/api/v1/histroyScore", middleware.jwt);
app.use("/api/v1", customMiddleware, user)
app.use("/api/v1", customMiddleware, ranking)



app.use((err, req, res, next) => {// 错误中间件
    res.send(err)
    next()
})

app.listen(9090, () => {
    console.log("run http://localhost")
})